package sh.wn36.vehiclemanage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import sh.wn36.vehiclemanage.controller.voe.CarQuery;
import sh.wn36.vehiclemanage.entity.Car;
import sh.wn36.vehiclemanage.entity.CurrentUnit;
import sh.wn36.vehiclemanage.entity.Dictionary;
import sh.wn36.vehiclemanage.service.CarService;
import sh.wn36.vehiclemanage.service.CurrentUnitService;
import sh.wn36.vehiclemanage.service.DictionaryService;
import sh.wn36.vehiclemanage.util.Result;
import sh.wn36.vehiclemanage.vo.carInfo;
import sh.wn36.vehiclemanage.vo.driverInfo;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xk
 * @since 2021-04-26
 */
@CrossOrigin
@Api(value = "车辆信息")
@RestController
@RequestMapping("/car")
public class CarController {

    @Autowired
    @ApiModelProperty("成员变量 carService")
    private CarService carService;

    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private CurrentUnitService currentUnitService;

    @ApiModelProperty("通过条件分页查询驾驶员")
    @PostMapping("/pageListCar/{current}/{limit}")
    public Result pageListDriver(@ApiParam(value = "当前页码",name = "currentPage",required = true)@PathVariable("current")Integer current,
                                 @ApiParam(value = "每页显示记录数",name = "pageSize",required = true)@PathVariable("limit")Integer limit,
                                 @ApiParam(value = "查询条件",name = "carQuery",required = false)@RequestBody(required = false) CarQuery carQuery){

        Page<carInfo> page = new Page<>(current,limit);
        QueryWrapper<carInfo> wrapper = new QueryWrapper<>();

        if(carQuery != null){
            String carNo = carQuery.getCarNo();
            if(!StringUtils.isEmpty(carNo)){
                wrapper.like("car_no",carNo);
            }
        }
        wrapper.orderByDesc("gmt_modified");
        IPage<carInfo> carInfo1 = carService.getAll1(page,wrapper);
        long total = page.getTotal();
        List<carInfo> carInfo = carInfo1.getRecords();
        return Result.OK().data("total",total).data("items",carInfo);

        }

    @ApiModelProperty("删除车辆信息")
    @RequestMapping("/deleteCar/{ids}")
    public Result deleteByIds(@PathVariable("ids") int[] ids){
        for (int i = 0; i < ids.length; i++) {
            int id = ids[i];
            carService.removeById(id);
        }
        return Result.OK();
    }

    @ApiModelProperty("通过pid拿到所有的字典内容")
    @GetMapping("/getDicById/{id}")
    public Result getBrandById(@PathVariable("id") Integer id){
        List<Dictionary> list = dictionaryService.getListByPid(id);
        return Result.OK().data("items", list);
    }

    @ApiModelProperty("通过pid拿到所有的字典内容")
    @GetMapping("/getDic/{id}")
    public Result getDicById(@PathVariable("id") Integer id){
        List<Dictionary> list = dictionaryService.getListByPid(id);
        return Result.OK().data("items", list);
    }


    @ApiModelProperty("查询供应商")
    @PostMapping("/getSuper")
    public Result getSuper(){
        List<CurrentUnit> list = currentUnitService.list(null);
        return Result.OK().data("items",list);
    }

    @ApiModelProperty("保存车辆信息")
    @PostMapping("/saveCar")
    public Result saveCar( @ApiParam(value = "前端返回的数据",name = "car",required = true)@RequestBody Car car){

        boolean result = carService.save(car);
        if (result)
            return Result.OK();
        else
            return Result.Fail();
    }

    @ApiModelProperty("通过id查找车辆信息")
    @GetMapping("/findCarById/{id}")
   public Result findCarById(@PathVariable("id")Integer id){
        Car car = carService.getById(id);
        return Result.OK().data("car",car);

    }


}



